package com.hzspace.elasticsearch.component;

import com.hzspace.elasticsearch.entity.Product;
import com.hzspace.elasticsearch.repository.ProductRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductService {

    @Resource
    private ProductRepository productRepository;


    public Page<Product> search(String name, Pageable pageable) {
//        QueryStringQueryBuilder query = new QueryStringQueryBuilder(name);
//        query.analyzer("ik_max_word");
//        query.field("name");
//        NativeSearchQuery searchQuery = new NativeSearchQuery(query);
//        return productRepository.search(query, pageable);
//        return productRepository.search(searchQuery);
        return productRepository.findByName(name, pageable);
    }

    public List<Product> search(String name) {
        return productRepository.findByName(name);
    }

    public Iterable<Product> getAll() {
        return productRepository.findAll();
    }

    public void init() {
        //init一些测试数据
//        List<MollyboxSpu> spus = mollyboxSpuMapper.selectAll();
//        List<Product> products = new ArrayList();
//        spus.forEach(spu -> {
//            Product product = new Product();
//            product.setId(spu.getId());
//            product.setName(spu.getName());
//            product.setTag(spu.getSpuTabName());
//            products.add(product);
//        });
//        productRepository.saveAll(products);
    }
}